﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using FrankJie.IDAL;
using FrankJie.Model;

namespace FrankJie.SQLServerDAL
{
	/// <summary>
	/// 标签数据访问类
	/// </summary>
	public partial class Tag:ITag
	{
        /// <summary>
        /// 增加一个标签
        /// </summary>
        /// <param name="tagInfo">标签实体</param>
        /// <returns>返回标签ID</returns>
        public int Add(TagInfo tagInfo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tblTag(");
			strSql.Append("TagName,HaveCount)");
			strSql.Append(" values (");
			strSql.Append("@TagName,@HaveCount)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@TagName", SqlDbType.VarChar,50),
					new SqlParameter("@HaveCount", SqlDbType.Int,4)};
            parameters[0].Value = tagInfo.TagName;
            parameters[1].Value = tagInfo.HaveCount;

			object obj = SQLHelper.GetSingle(SQLHelper.FrankJieDBConnStr,CommandType.Text,strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

        /// <summary>
        /// 增加标签所拥有的数量
        /// </summary>
        /// <param name="id">标签ID</param>
        /// <returns>返回是否成功</returns>
        public bool AddCount(int id)
        {
            string strSql = "UPDATE tblTag SET HaveCount=HaveCount+1 WHERE Id=@Id";
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            int rows = SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 减少标签所拥有的数量
        /// </summary>
        /// <param name="id">标签ID</param>
        /// <returns>返回是否成功</returns>
        public bool DeductCount(int id)
        {
            string strSql = "UPDATE tblTag SET HaveCount=HaveCount-1 WHERE Id=@Id";
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            int rows = SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 删除标签通过ID
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public bool Delete(int id)
		{
            string strSql = "DELETE FROM tblTag WHERE Id=@Id";
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = SQLHelper.ExecuteSql(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        /// <summary>
        /// 获取标签列表
        /// </summary>
        /// <returns>返回标签集合</returns>
        public List<TagInfo> GetList()
        {
            string strSql = "SELECT * FROM tblTag";
            List<TagInfo> tagInfoList = new List<TagInfo>();
            TagInfo tagInfo = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.FrankJieDBConnStr, CommandType.Text, strSql))
            {
                tagInfo = new TagInfo();
                tagInfo.Id = int.Parse(dr["Id"].ToString());
                tagInfo.TagName = dr["TagName"].ToString();
                tagInfo.HaveCount = int.Parse(dr["HaveCount"].ToString());
                tagInfoList.Add(tagInfo);
            }
            return tagInfoList;
        }
	}
}

